package academic.concordia.bankofquebec.managedbean;

import java.util.ArrayList;
import java.util.List;

import javax.faces.bean.ManagedProperty;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.GrantedAuthorityImpl;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.stereotype.Component;

import academic.concordia.bankofquebec.bean.authentication.MyAuthenticationToken;
import academic.concordia.bankofquebec.bean.client.IClientBean;
import academic.concordia.bankofquebec.bean.session.SessionData;
import academic.concordia.bankofquebec.entity.Account;
import academic.concordia.bankofquebec.entity.AccountType;
import academic.concordia.bankofquebec.entity.BankAccount;
import academic.concordia.bankofquebec.entity.CheckingsAccount;
import academic.concordia.bankofquebec.entity.Client;
import academic.concordia.bankofquebec.entity.CreditCard;
import academic.concordia.bankofquebec.entity.IdentificationType;

@Component
@Scope("session")
public class CustomAuthenticationProvider implements AuthenticationProvider {
	
	IClientBean clientBean;
	
	private SessionData sessionData;
	
	@SuppressWarnings("deprecation")
	@Override
	public Authentication authenticate(Authentication authentication)
			throws AuthenticationException {
		
		String clientInfo = authentication.getName();
        String password = authentication.getCredentials().toString();
        Client reqClient = new Client();
        IdentificationType idType = IdentificationType.PASSPORT;
        reqClient.setIdentificationType(idType);
        reqClient.setIdNumber(clientInfo.split("-")[1]);
        reqClient.setPassword(password);
        System.out.print("clientBean: " + clientBean);
        Client authClient = clientBean.authenticateClient(reqClient);
        
        if (authClient != null) {
            List<GrantedAuthority> grantedAuths = new ArrayList<GrantedAuthority>();
            grantedAuths.add(new SimpleGrantedAuthority("ROLE_USER"));
            Authentication auth = new UsernamePasswordAuthenticationToken(clientInfo, password, grantedAuths);
            
            //this.sessionData = new SessionData();
            this.sessionData.setClient(authClient);
            
            List<GrantedAuthority> AUTHORITIES = new ArrayList<GrantedAuthority>();
            AUTHORITIES.add(new GrantedAuthorityImpl("ROLE_USER"));
            
            return new MyAuthenticationToken(authentication.getName(), authentication.getCredentials(), AUTHORITIES, authClient);
        } else {
            return null;
        }
	}

	@Override
	public boolean supports(Class<?> authentication) {
		return (UsernamePasswordAuthenticationToken.class.isAssignableFrom(authentication));
	}

	public IClientBean getClientBean() {
		return clientBean;
	}

	public void setClientBean(IClientBean clientBean) {
		this.clientBean = clientBean;
	}

	public SessionData getSessionData() {
		return sessionData;
	}

	public void setSessionData(SessionData sessionData) {
		this.sessionData = sessionData;
	}
}
